package 牛客_面试必刷.Month07.day10;

import java.util.ArrayList;
import java.util.List;

public class 二叉树的层序遍历 {

    ArrayList<ArrayList<Integer>> list = new ArrayList();
    int treeHeight = 0;
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        // write code here
        levelOrder(root,0);
        return list;
    }

    public void levelOrder(TreeNode root,int height){
        if(root == null) return;
        if(height > treeHeight){
            list.add(new ArrayList<>());
            treeHeight++;
        }
        list.get(height).add(root.val);
        levelOrder(root.left,height+1);
        levelOrder(root.right,height+1);
    }
}
